package lib.persistence;

import global.ConfigData;
import global.ConfigDataTest;

import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

//import com.sun.tools.internal.jxc.gen.config.Config;

import lib.Field;
import lib.persistence.commands.InsertRow;
import lib.reflection.Reflector;
import models.Deliverable;
import models.Event;
import models.Milestone;
import models.Person;
import models.Project;
import models.Publication;

/**
 * Class takes in an ExcelConnector and returns a project
 * @author jon
 *
 */
public class ProjectFactory  
{
	private ExcelConnector excelConnector;

	private String worksheetProject 	= "Project";
	private String worksheetPerson 		= "Person";
	private String worksheetMilestone 	= "Milestone";
	private String worksheetDeliverable = "Deliverable";
	private String worksheetEvent		= "Event";
	private String worksheetPublication = "Publication";
	List<Person> people = new ArrayList<Person>();
	
	public ProjectFactory(ExcelConnector ec)
	{
		excelConnector = ec;
		
	}

	public Project getProjectFromExcelRow(int rowNumber) 
	{
		mapPeopleFromExcelToDatabase();	
		Project project = mapProjectFromExcelToDatabase(rowNumber);		
		mapDeliverableFromExcelToDatabase();
		mapPublicationsFromExcelToDatabase();
		mapEventsFromExcelToDatabase();
		mapMilestonesFromExcelToDatabase();
		
		// we assume all people in the spreadsheet are associated with the project:
		mapPeopleToProject(project);	

		return project;
	}

	private void mapPeopleToProject(Project project) {
		List<Person> actors = new ArrayList<Person>();
		for (Person person : people) 
		{
			if (!project.getActors().contains(person))
				actors.add(person);
			
		}
		try {
			project.setActors(actors);
			project.update();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	private void mapMilestonesFromExcelToDatabase() {
		excelConnector.setWorksheet(worksheetMilestone);
		List<List<Field>> rows = excelConnector.getAllRows();
		
		for (List<Field> row : rows) {
			Milestone event = new Milestone();
			try {
				event =  (Milestone) Reflector.setReflectedMethods(row, event, Class.forName("models.Milestone"));
				event.create();
			} catch (Exception e) {
				e.printStackTrace();
			} 
		}		
		
	}

	private void mapEventsFromExcelToDatabase() {
		excelConnector.setWorksheet(worksheetEvent);
		List<List<Field>> rows = excelConnector.getAllRows();
		
		for (List<Field> row : rows) {
			Event event = new Event();
			try {
				event =  (Event) Reflector.setReflectedMethods(row, event, Class.forName("models.Event"));
				event.create();
			} catch (Exception e) {
				e.printStackTrace();
			} 
		}		
	}

	private void mapPublicationsFromExcelToDatabase() {
		excelConnector.setWorksheet(worksheetPublication);
		List<List<Field>> rows = excelConnector.getAllRows();
		
		for (List<Field> row : rows) {
			Publication publication = new Publication();
			try {
				publication =  (Publication) Reflector.setReflectedMethods(row, publication, Class.forName("models.Publication"));
				publication.create();
			} catch (Exception e) {
				e.printStackTrace();
			} 
		}		
	}

	private void mapDeliverableFromExcelToDatabase() {
		excelConnector.setWorksheet(worksheetDeliverable);
		List<List<Field>> rows = excelConnector.getAllRows();
		
		for (List<Field> row : rows) {
			Deliverable deliverable = new Deliverable();
			try {
				deliverable =  (Deliverable) Reflector.setReflectedMethods(row, deliverable, Class.forName("models.Deliverable"));
				deliverable.create();
			} catch (Exception e) {
				System.out.println("Deliverable not mapped yet: " + deliverable.getId());
			} 
		}
	}

	private Project mapProjectFromExcelToDatabase(int rowNumber) {
		excelConnector.setWorksheet(worksheetProject);
		List<Field> fields = excelConnector.getRow(rowNumber);

		Project project = new Project();
		try {
			project = (Project) Reflector.setReflectedMethods(fields, project, Class.forName("models.Project"));
			project.create();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return project;
	}

	private void mapPeopleFromExcelToDatabase() {
		excelConnector.setWorksheet(worksheetPerson);
		List<List<Field>> rows = excelConnector.getAllRows();
		
		people = new ArrayList<Person>();
		for (List<Field> row : rows) {
			Person person = new Person();
			try {
				person =  (Person) Reflector.setReflectedMethods(row, person, Class.forName("models.Person"));
				person.create();
				people.add(person);
			} catch (Exception e) {
				e.printStackTrace();
			} 
		}
		System.out.println("done people");
	}

	public void getProjects() {
		excelConnector.setWorksheet(worksheetProject);
		List<List<Field>> rows = excelConnector.getAllRows();
		
		for (int i = 1; i < rows.size(); i++) 
		{
			getProjectFromExcelRow(i); 
		}
		
	}
}
